﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Drawing.Design;
using System.ComponentModel;
using System.Windows.Forms.Design;
using System.Windows.Forms.Integration;

namespace comm.editor
{
    public class CreateUnitEditor : UITypeEditor
    {
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.DropDown;
        }
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            var edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
            if (edSvc != null)
            {
                var elHost = new ElementHost
                {
                    Width = 360,
                    Height = 420,
                    Child = new UCCreateUnit((List<comm.objst.CreateUnit>)value)
                };
                edSvc.DropDownControl(elHost);
                return value;
            }
            return value;
        }
    }

    /// <summary>
    /// UCCreateUnit.xaml 的交互逻辑
    /// </summary>
    public partial class UCCreateUnit : UserControl
    {
        List<comm.objst.CreateUnit> _createList = null;

        public UCCreateUnit(List<comm.objst.CreateUnit> createList)
        {
            InitializeComponent();

            _createList = createList;

            var names = comm.EnumHelper.GetNames(typeof(objst.UnitType));
            cbUnitType.Items.Add("null");
            foreach (var name in names)
            {
                cbUnitType.Items.Add(name);
            }
            cbUnitType.SelectionChanged += new SelectionChangedEventHandler(cbUnitType_SelectionChanged);
            if (cbUnitType.Items.Count > 0)
            {
                cbUnitType.SelectedIndex = 0;
            }

            Fill();

            btnAdd.Click += new RoutedEventHandler(btnAdd_Click);
            btnClear.Click += new RoutedEventHandler(btnClear_Click);
        }

        void btnClear_Click(object sender, RoutedEventArgs e)
        {
            _createList.Clear();
            Fill();
        }
        void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            if (cbUnit.SelectedIndex != -1)
            {
                var tb = (TextBlock)cbUnit.SelectedItem;
                var unitId = tb.Tag.ToString();
                _createList.Add(new objst.CreateUnit { UnitId = unitId, Count = 1 });
                Fill();
            }
        }
        void cbUnitType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cbUnitType.SelectedIndex != -1)
            {
                objst.UnitType? unitType = null;
                if (cbUnitType.SelectedItem.ToString() != "null")
                {
                    unitType = comm.EnumHelper.Parse<objst.UnitType>(cbUnitType.SelectedItem.ToString());
                }
                var units = DefineFile.LastInstance.GetAllUnit(unitType);
                cbUnit.Items.Clear();
                foreach (var unitId in units)
                {
                    var tb = new TextBlock
                    {
                        Tag = unitId,
                        Text = unitId
                    };
                    cbUnit.Items.Add(tb);
                }
                if (cbUnit.Items.Count > 0)
                {
                    cbUnit.SelectedIndex = 0;
                }
            }
        }

        void Fill()
        {
            spList.Children.Clear();

            var list = new List<objst.CreateUnit>();
            foreach (var item in _createList)
            {
                if (DefineFile.LastInstance.GetUnit(item.UnitId) != null)
                {
                    list.Add(item);
                }
            }
            _createList.Clear();
            _createList.AddRange(list);
            foreach (var item in _createList)
            {
                var item2 = item;
                var grid = new Grid { Margin = new Thickness(0, 0, 0, 4) };

                #region tbKey
                var tbKey = new TextBlock
                {
                    Text = item.UnitId,
                    Width = 120,
                    HorizontalAlignment = HorizontalAlignment.Left,
                    VerticalAlignment = VerticalAlignment.Center
                };
                grid.Children.Add(tbKey);
                #endregion

                #region tbLevel
                var tbLevel = new TextBox
                {
                    Text = item.Count.ToString(),
                    Width = 80,
                    Margin = new Thickness(120, 0, 24, 0),
                    HorizontalAlignment = HorizontalAlignment.Left
                };
                tbLevel.TextChanged += new TextChangedEventHandler((o, e) =>
                {
                    uint n = 0;
                    if (uint.TryParse(tbLevel.Text, out n))
                    {
                        item2.Level = n;
                        tbLevel.Opacity = 1;
                    }
                    else
                    {
                        tbLevel.Opacity = .5;
                    }
                });
                grid.Children.Add(tbLevel);
                #endregion

                #region tbCount
                var tbCount = new TextBox
                {
                    Text = item.Count.ToString(),
                    Margin = new Thickness(204, 0, 24, 0),
                    VerticalAlignment = VerticalAlignment.Center
                };
                tbCount.TextChanged += new TextChangedEventHandler((o, e) =>
                {
                    uint n = 0;
                    if (uint.TryParse(tbCount.Text, out n))
                    {
                        item2.Count = n;
                        tbCount.Opacity = 1;
                    }
                    else
                    {
                        tbCount.Opacity = .5;
                    }
                });
                grid.Children.Add(tbCount);
                #endregion

                #region btnDel
                var btnDel = new Button
                {
                    Content = "X",
                    Width = 22,
                    Height = 22,
                    VerticalAlignment = VerticalAlignment.Center,
                    HorizontalAlignment = HorizontalAlignment.Right
                };
                btnDel.Click += new RoutedEventHandler((o, e) =>
                {
                    _createList.Remove(item2);
                    Fill();
                });
                grid.Children.Add(btnDel);
                #endregion

                spList.Children.Add(grid);
            }
        }
    }
}
